package com.yuqih.common.service.view.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.stereotype.Service;

import com.yuqih.common.service.view.IViewConverter;

@Service
public class DateViewConverter implements IViewConverter {
	
	
	private final Map<String, SimpleDateFormat> formaters = new ConcurrentHashMap<String, SimpleDateFormat>();
	
	@Override
	public String alias() {
		return Alias_Date_Format;
	}

	@Override
	public Object convert(Object source, Context context) {
		
		if (context.isList()) {
			throw new IllegalArgumentException("暂不支持时间列表");
		} 
		
		if (source == null) {
			return null;
		}
		
		SimpleDateFormat formater = formaters.get(context);
		if (formater == null) {
			formater = new SimpleDateFormat(context.getParam());
			formaters.put(context.getParam(), formater);
		}
		
		return formater.format((Date)source);
	}

}
